package com.winai.launcher.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.winai.launcher.R;
import com.winai.launcher.base.BaseRecAdapter;
import com.winai.launcher.base.BaseRecViewHolder;
import com.winai.launcher.bean.BluetoothItemBean;
import com.winai.launcher.bean.BluetoothTitleBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：康少
 * 时间：2019/11/21 0021
 * 说明：内部类——蓝牙title
 */
public class TitleAdapter extends BaseRecAdapter<BluetoothTitleBean, TitleAdapter.ViewHolder> {
    private Context mContext;
    private List<BluetoothItemBean> switchBluetoothList = new ArrayList<>();//蓝牙开关item
    private RecyclerView innerRecyclerView;
    private BluetoothItemAdapter bindBluetoothItemAdapter;
    private BluetoothItemAdapter unbindBluetoothItemAdapter;

    public TitleAdapter(Context mContext, BluetoothItemAdapter bindBluetoothItemAdapter
            , BluetoothItemAdapter unbindBluetoothItemAdapter) {
        this.bindBluetoothItemAdapter = bindBluetoothItemAdapter;
        this.unbindBluetoothItemAdapter = unbindBluetoothItemAdapter;
        this.mContext = mContext;
        switchBluetoothList.add(new BluetoothItemBean(
                0
                , "蓝牙"
                , BluetoothItemBean.BluetoothState.SWITCH
                , null
        ));
    }

    @Override
    public void onHolder(ViewHolder holder, BluetoothTitleBean bean, int position) {
        TextView wifiTitle = holder.getView(R.id.str_wifiTitle);
        TextView currentDeviceName = holder.getView(R.id.tv_currentDeviceName);
        wifiTitle.setText(bean.getTitleName());
        if (bean.getCurrentDeviceName() != null && !bean.getCurrentDeviceName().equals("")) {
            currentDeviceName.setVisibility(View.VISIBLE);
            String Str = mContext.getResources().getString(R.string.bluetoothCurrentDeviceName);
            String ss = String.format(Str, bean.getCurrentDeviceName());
            currentDeviceName.setText(ss);
        } else {
            currentDeviceName.setVisibility(View.GONE);
        }

        innerRecyclerView = holder.getView(R.id.recyclerView_inner);
        innerRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        if (bean.getTitleId() == -1) {
            BluetoothItemAdapter bluetoothItemAdapter = new BluetoothItemAdapter(mContext
                    ,false, R.layout.item_bluetooth_switch);
            innerRecyclerView.setAdapter(bluetoothItemAdapter);
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    bluetoothItemAdapter.addDataList(switchBluetoothList);
                }
            });
        } else if (bean.getTitleId() == -2) {
            innerRecyclerView.setAdapter(bindBluetoothItemAdapter);
        } else if (bean.getTitleId() == -3) {
            innerRecyclerView.setAdapter(unbindBluetoothItemAdapter);
        }
    }

    @Override
    public ViewHolder onCreateHolder() {
        return new ViewHolder(getViewByRes(R.layout.item_set_wifi));
    }

    class ViewHolder extends BaseRecViewHolder {
        ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
